package client.role.button;

import client.common.result.Result;
import client.common.vo.RoleVO;
import client.common.vo.UserVO;
import client.role.UserList;
import remote.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class ButtonEditor extends DefaultCellEditor {
    private String label;
    private JButton button;
    private boolean clicked;
    private List<UserVO> userList;
    private List<RoleVO> roleListFromServer;
    private UserService userService;
    private JTable table; // 保存表格引用
    private UserList userListUI; // 保存 UserList 的引用
    
    // 构造函数增加 userListUI 参数
    public ButtonEditor(JCheckBox checkBox, List<UserVO> userList, List<RoleVO> roleListFromServer, UserService userService, UserList userListUI) {
        super(checkBox);
        this.userList = userList;
        this.roleListFromServer = roleListFromServer;
        this.userService = userService;
        this.userListUI = userListUI; // 初始化 userListUI
        button = new JButton();
        button.setOpaque(true);
        button.setText("修改角色");
        button.addActionListener(e -> {
            if (clicked && table != null) {
                // 弹出一个选择框，选择新的角色类型
                String[] roleOptions = roleListFromServer.stream().map(RoleVO::getName).toArray(String[]::new);
                String newRole = (String) JOptionPane.showInputDialog(null, "选择新角色类型：", "修改角色", JOptionPane.QUESTION_MESSAGE, null, roleOptions, roleOptions[0]);
                
                if (newRole != null) {
                    // 根据选中的角色类型进行更新操作
                    Integer newRoleId = getRoleIdByRoleName(newRole);
                    UserVO selectedUser = userList.get(table.getSelectedRow());
                    Result<String> updateResult = userService.updateUserRole(Long.valueOf(selectedUser.getId()), newRoleId);
                    if (updateResult.getCode() == 200) {
                        JOptionPane.showMessageDialog(null, "角色修改成功！");
                        // 调用 freshTable 方法刷新表格
                        userListUI.freshTable();
                    } else {
                        JOptionPane.showMessageDialog(null, "角色修改失败：" + updateResult.getMessage());
                    }
                }
            }
            fireEditingStopped(); // 停止编辑
        });
    }
    
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        // 保存 table 引用，供后续使用
        this.table = table;
        clicked = true;
        return button;
    }
    
    @Override
    public Object getCellEditorValue() {
        return label;
    }
    
    @Override
    public boolean stopCellEditing() {
        clicked = false;
        return super.stopCellEditing();
    }
    
    private Integer getRoleIdByRoleName(String roleName) {
        for (RoleVO role : roleListFromServer) {
            if (role.getName().equals(roleName)) {
                return role.getId();
            }
        }
        return null;
    }
}
